package org.example.maven.museumuser.controller;

import org.example.maven.museumuser.domain.pojo.Result;
import org.example.maven.museumuser.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class SearchController {
    @Autowired
    private SearchService searchService;

    @GetMapping("/search/{value}")
    @PreAuthorize("permitAll()")
    public Result search(@PathVariable String value) {
        Result result = Result.success();
        List<Map<String,String>>exhibitionList = new ArrayList<>();
        List<Map<String,String>>collectionList = new ArrayList<>();
        for(String s:searchService.searchExhibition(value)){
            Map<String,String> exhibition = new HashMap<>();
            exhibition.put("name", s);
            exhibitionList.add(exhibition);
        }
        for(String s:searchService.searchCollection(value)){
            Map<String,String> collection = new HashMap<>();
            collection.put("name", s);
            collectionList.add(collection);
        }
        result.put("exhibition", exhibitionList);
        result.put("collection", collectionList);
        return result;
    }
}
